package com.muc.plugin.task;

import java.util.Collection;
import java.util.List;
import java.util.TimerTask;

import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.session.ClientSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;

import com.muc.plugin.dao.MUCDao;
import com.muc.plugin.vo.MemberJoinVO;


/**
 * 用户把某某添加到房间的通知信息
 * 暫時用不到
 */
public class MUCJoinBoardCastTask extends TimerTask {

    private static final Logger log = LoggerFactory.getLogger(MUCJoinBoardCastTask.class);
    private JID roomJid;
    private JID fromJid;
    private List<MemberJoinVO> members;

    @Override
    public void run() {
        hander();
    }

    public MUCJoinBoardCastTask(JID roomJid, List<MemberJoinVO> members, JID fromJid) {
        this.roomJid = roomJid;
        this.members = members;
        this.fromJid = fromJid;
    }

    private void hander() {
        for (MemberJoinVO jidnode : members) {
            if (jidnode == null || jidnode.getJid() == null || jidnode.getNickname() == null) {
                continue;
            }
            MUCRoom mucroom = XMPPServer.getInstance().getMultiUserChatManager().
                    getMultiUserChatService(roomJid).getChatRoom(roomJid.getNode());
            IQ iq = new IQ(IQ.Type.set);
            Element frag = iq.setChildElement("query", "http://jabber.org/protocol/muc#admin");
            Element item = frag.addElement("item");
            item.addAttribute("affiliation", "member");
            item.addAttribute("jid", jidnode.getJid());
            item.addAttribute("nick", jidnode.getNickname());
            try {
                mucroom.getIQAdminHandler().handleIQ(iq, mucroom.getRole());
                // 修改昵称?
                MUCDao.updateNick(mucroom.getID(), jidnode.getJid(), jidnode.getNickname());
                // 通知用户
                this.sendJoinResultIQ(jidnode.getJid(), mucroom.getID() + "", mucroom.getDescription(), mucroom.getNaturalLanguageName());
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    /**
     * 发送对应的IQ
     *
     * @param jidnode
     */
    public void sendJoinResultIQ(String jidnode, String roomid, String desc, String naturalName) {
        log.info("execute " + this.getClass().getName() + " method: sendJoinResultIQ");
        //检测  to 的用户是否在线
        String to = jidnode.substring(0, jidnode.indexOf("@"));
        Collection<ClientSession> sessions = SessionManager.getInstance().getSessions(to);
        for (ClientSession session : sessions) {
            IQ iq = new IQ(Type.result);
            iq.setChildElement("query", "xls:muc-join-member");
            iq.getChildElement().addElement("item").addAttribute("roomjid", roomJid.toBareJID())
                    .addAttribute("roomid", roomid).addAttribute("description", desc).addAttribute("from", fromJid.toBareJID())
                    .addAttribute("naturalname", naturalName);
            iq.setTo(session.getAddress());
            iq.setFrom(roomJid.getDomain());
            log.info("用户在线！发送添加用户到房间的通知信息: " + iq.toXML());
            session.process(iq);
        }
    }


}
